package com.itheima.controller;



import com.itheima.common.PageResults;



import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest request;


    /**
     *
     * @param user
     */
    //用户修改
    @PutMapping("/{id}")

    public void modifyById(@RequestBody User user) {
        System.out.println("回想" + user);
        userService.modifyById(user);
    }

    /**
     *
     * @param page
     * @param pagesize
     * @param username
     * @return
     */
    //用户列表
    @GetMapping
    public PageResults page(Integer page , Integer pagesize , String username){
        PageResults pageResults=userService.page(page,pagesize,username);

        return pageResults;
    }

    /**
     *
     * @param id
     */
    //用户删除
    @DeleteMapping("/{id}")
    public void updateById(@PathVariable Integer id){


        userService.delete(id);
    }

    //用户屏蔽、启用
    @PutMapping("/{id}/disabled")
    public boolean disabled(@PathVariable Integer id){

       return userService.disabled(id);

    }


    /**
     *
     * @param user
     */
    //用户添加
    @PostMapping
    public void add(@RequestBody User user){
        System.out.println("参数"+user);
         userService.add(user);
    }


    /**
     *
     * @param
     * @return
     */


    //用户简单列表
    @GetMapping("/simple")
    public List selectLists(){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List list = userService.selectList(user);
        return list;

    }



    /**
     *
     * @param id
     * @return
     */

    //用户详情(回显)
    @GetMapping("/{id}")
    public User get(@PathVariable Integer id){

        return  userService.get(id);
    }

    /**
     *
     * @param id
     * @param password
     */
    //用户重置密码
    @PutMapping("/{id}/password")
    public void Reset(@PathVariable Integer id,String password){

        userService.reset(id,password);

    }

}
